package com.party.core.service.crowdfund.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.activity.ActivityReadDao;
import com.party.core.dao.read.crowdfund.CrowdfundEventReadDao;
import com.party.core.dao.read.crowdfund.ProjectReadDao;
import com.party.core.dao.write.crowdfund.CrowdfundEventWriteDao;
import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.activity.Activity;
import com.party.core.model.city.Area;
import com.party.core.model.city.City;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.crowdfund.CrowdfundEventCount;
import com.party.core.model.crowdfund.CrowdfundEventWithMemberPartner;
import com.party.core.model.crowdfund.Project;
import com.party.core.service.city.IAreaService;
import com.party.core.service.city.ICityService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 众筹事件接口实现
 * Created by wei.li
 *
 * @date 2017/4/25 0025
 * @time 19:25
 */

@Service
public class CrowdfundEventService implements ICrowdfundEventService {

    @Autowired
    private CrowdfundEventReadDao crowdfundEventReadDao;

    @Autowired
    private CrowdfundEventWriteDao crowdfundEventWriteDao;

    @Autowired
    private ActivityReadDao activityReadDao;

    @Autowired
    private ProjectReadDao projectReadDao;



    /**
     * 插入众筹事件
     * @param crowdfundEvent 众筹事件
     * @return 事件编号
     */
    @Override
    public String insert(CrowdfundEvent crowdfundEvent) {
        BaseModel.preInsert(crowdfundEvent);
        boolean result = crowdfundEventWriteDao.insert(crowdfundEvent);
        if (result){
            return crowdfundEvent.getId();
        }
        return null;
    }

    /**
     * 更新众筹事件
     * @param crowdfundEvent 众筹事件
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(CrowdfundEvent crowdfundEvent) {
        if (null != crowdfundEvent.getUpdateDate()) {
            crowdfundEvent.setUpdateDate(new Date());
        }
        return crowdfundEventWriteDao.update(crowdfundEvent);
    }

    /**
     * 逻辑删除众筹事件
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return crowdfundEventWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除众筹事件
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return crowdfundEventWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除众筹事件
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return crowdfundEventWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除众筹事件
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return crowdfundEventWriteDao.batchDelete(ids);
    }

    /**
     * 根据编号获取
     * @param id 主键
     * @return 众筹事件
     */
    @Override
    public CrowdfundEvent get(String id) {
        return crowdfundEventReadDao.get(id);
    }

    /**
     * 众筹事件列表
     * @param crowdfundEvent 众筹事件
     * @param page 分页信息
     * @return 事件列表
     */
    @Override
    public List<CrowdfundEvent> listPage(CrowdfundEvent crowdfundEvent, Page page) {
        return crowdfundEventReadDao.listPage(crowdfundEvent, page);
    }

    /**
     * 所有众筹事件列表
     * @param crowdfundEvent 众筹事件
     * @return 事件列表
     */
    @Override
    public List<CrowdfundEvent> list(CrowdfundEvent crowdfundEvent) {
        return crowdfundEventReadDao.listPage(crowdfundEvent, null);
    }

    /**
     * 所有众筹事件列表, 带有其他的筛选条件的
     * @param crowdfundEvent 众筹事件
     * @param params
     * @return 事件列表
     */
    @Override
    public List<CrowdfundEvent> listWithParams(CrowdfundEvent crowdfundEvent, Map<String, Object> params) {
        return crowdfundEventReadDao.listWithParams(crowdfundEvent, params);
    }

    /**
     * 批量查询众筹事件
     * @param ids 主键集合
     * @param crowdfundEvent 众筹事件
     * @param page 分页信息
     * @return 事件列表
     */
    @Override
    public List<CrowdfundEvent> batchList(@NotNull Set<String> ids, CrowdfundEvent crowdfundEvent, Page page) {
        return null;
    }

    @Override
    public List<CrowdfundEventWithMemberPartner> webListPage(CrowdfundEventWithMemberPartner crowdfundEvent, Map<String, Object> params, Page page) {
        return crowdfundEventReadDao.webListPage(crowdfundEvent, params, page);
    }

    /**
     * 是否报名众筹主题
     * @param id 活动编号
     * @param authorId 发布者
     * @return 是否报名(true/false)
     */
    @Override
    public boolean isApplyEvent(String id, String authorId) {
        Activity activity = activityReadDao.get(id);
        if (null == activity){
            throw new BusinessException("众筹活动不存在");
        }
        if (Strings.isNullOrEmpty(activity.getEventId())){
            return false;
        }

        List<Project> list = projectReadDao.findByEvent(activity.getEventId(), id, authorId);
        if (CollectionUtils.isEmpty(list)){
            return false;
        }
        return true;
    }


    @Override
    public CrowdfundEventCount getEventCountResult(String eventId) {
        if (eventId == null || eventId.isEmpty())
            return null;
        CrowdfundEventCount eventCount = crowdfundEventReadDao.getEventCountResult(eventId);
        return null == eventCount ? new CrowdfundEventCount() : eventCount;
    }

    /**
     * 根据项目id查主题
     * @param targetId
     * @return
     */
    @Override
    public CrowdfundEvent getCorwdfundByActivityId(String targetId) {
        return crowdfundEventReadDao.getCorwdfundByActivityId(targetId);
    }

    /**
     * 根据音频查主题
     * @param crowdfundEvent
     * @return
     */
    @Override
    public List<CrowdfundEvent> findEventByAudio(CrowdfundEvent crowdfundEvent) {
        return crowdfundEventReadDao.findEventByAudio(crowdfundEvent);
    }

    @Override
    public CrowdfundEvent findByProjectId(String projectId) {
        return crowdfundEventReadDao.findByProjectId(projectId);
    }

    /**找到所有多场没有截止的的活动
    * @author      hzq
    * @param
    * @date        2019/4/10 16:55
    */
    @Override
    public List<CrowdfundEvent> getMultipleCrowdfundEvent() {
        return crowdfundEventReadDao.getMultipleCrowdfundEvent();
    }
    @Autowired
    ICityService cityService;
    @Autowired
    IAreaService areaService;

    @Override
    public String getShowAddress(CrowdfundEvent event) {
        City c = cityService.get(event.getCityId());
        String address = c != null ? c.getName() : "";
        Area area = areaService.get(event.getArea());
        if (area != null) {
            address += area.getName();
        } else {
            address += event.getArea() != null ? event.getArea() : "";
        }
        address += event.getPlace() != null ? event.getPlace() : "";
        return address;
    }

    @Override
    public List<CrowdfundEvent> getPriceIsNull() {
        return crowdfundEventReadDao.getPriceIsNull();
    }
}
